/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.zx.question.bank.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.utils.excel.annotation.ExcelField.Type;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.entity.Area;
import com.jeesite.modules.sys.entity.Post;
import com.jeesite.modules.sys.service.PostService;
import com.jeesite.modules.sys.utils.DictUtils;
import com.jeesite.modules.zx.mockexam.special.single.entity.TmockexamSpecialSingle;
import com.jeesite.modules.zx.question.bank.entity.TquestionBank;
import com.jeesite.modules.zx.question.bank.service.TquestionBankService;

/**
 * 题库管理Controller
 * @author zpp
 * @version 2020-03-05
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/question/bank/tquestionBank")
public class TquestionBankController extends BaseController {

	@Autowired
	private TquestionBankService tquestionBankService;
	@Autowired
	private PostService postService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TquestionBank get(String id, boolean isNewRecord) {
		return tquestionBankService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("zx:question:bank:tquestionBank:view")
	@RequestMapping(value = {"list", ""})
	public String list(TquestionBank tquestionBank, Model model) {
		Post post = new Post();
		model.addAttribute("postList", postService.findList(post));
		model.addAttribute("tquestionBank", tquestionBank);
		return "modules/zx/question/bank/tquestionBankList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("zx:question:bank:tquestionBank:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TquestionBank> listData(TquestionBank tquestionBank, HttpServletRequest request, HttpServletResponse response) {
		tquestionBank.setPage(new Page<>(request, response));
		Page<TquestionBank> page = tquestionBankService.findPage(tquestionBank);
		return page;
	}
	@RequiresPermissions("zx:question:bank:tquestionBank:view")
	@RequestMapping(value = "findList")
	@ResponseBody
	public List<TquestionBank> findList(TquestionBank tquestionBank, HttpServletRequest request, HttpServletResponse response) {
		List<TquestionBank> list = tquestionBankService.findList(tquestionBank);
		return list;
	}
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("zx:question:bank:tquestionBank:view")
	@RequestMapping(value = "form")
	public String form(TquestionBank tquestionBank, Model model) {
		Post post = new Post();
		model.addAttribute("postList", postService.findList(post));
		model.addAttribute("tquestionBank", tquestionBank);
		return "modules/zx/question/bank/tquestionBankForm";
	}

	/**
	 * 保存题库管理
	 */
	@RequiresPermissions("zx:question:bank:tquestionBank:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TquestionBank tquestionBank) {
		tquestionBankService.save(tquestionBank);
		return renderResult(Global.TRUE, text("保存题库管理成功！"));
	}
	
	/**
	 * 停用题库管理
	 */
	@RequiresPermissions("zx:question:bank:tquestionBank:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(TquestionBank tquestionBank) {
		tquestionBank.setStatus(TquestionBank.STATUS_DISABLE);
		tquestionBankService.updateStatus(tquestionBank);
		return renderResult(Global.TRUE, text("停用题库管理成功"));
	}
	
	/**
	 * 启用题库管理
	 */
	@RequiresPermissions("zx:question:bank:tquestionBank:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(TquestionBank tquestionBank) {
		tquestionBank.setStatus(TquestionBank.STATUS_NORMAL);
		tquestionBankService.updateStatus(tquestionBank);
		return renderResult(Global.TRUE, text("启用题库管理成功"));
	}
	
	/**
	 * 删除题库管理
	 */
	@RequiresPermissions("zx:question:bank:tquestionBank:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TquestionBank tquestionBank) {
		tquestionBankService.delete(tquestionBank);
		return renderResult(Global.TRUE, text("删除题库管理成功！"));
	}
	/**
	 * 下载导入用户数据模板
	 */
	@RequiresPermissions("zx:question:bank:tquestionBank:view")
	@RequestMapping(value = "importTemplate")
	public void importTemplate(HttpServletRequest request,HttpServletResponse response) {
		TquestionBank entity=new TquestionBank();
		entity.setPage(new Page<>(request, response));
		Page<TquestionBank> findPage = tquestionBankService.findPage(entity);        
		List<TquestionBank> list = findPage.getList();
		for(TquestionBank single:list){
			Integer objType = single.getObjType();
			String dictLabel = DictUtils.getDictLabel("t_exercises_query", objType.toString(), "未定义");
			single.setTypeName(dictLabel);
			String objAnswer = single.getObjAnswer();
			String answerName = DictUtils.getDictLabels("t_data_answer", objAnswer, "未定义");
			single.setAnswerName(answerName);
			String postCode = single.getPost();
			if(StringUtils.isNotBlank(postCode)){
				Post post= postService.get(postCode);
				single.setPostName(post.getPostName());
			}
			Integer category = single.getCategory();
			if(category!=null){
				String categoryName = DictUtils.getDictLabel("t_examination_category", category.toString(), "未定义");
				single.setCategoryName(categoryName);
			}
			Integer difficulty = single.getDifficulty();
			if(difficulty!=null){
				String difficultyName = DictUtils.getDictLabel("t_degree_difficulty", difficulty.toString(), "未定义");
				single.setDifficultyName(difficultyName);
			}
			Integer knowledgePointType = single.getKnowledgePointType();
			if(knowledgePointType!=null){
				String knowledgePointName = DictUtils.getDictLabel("t_knowledge_point_type", knowledgePointType.toString(), "未定义");
				single.setDifficultyName(knowledgePointName);
			}
		}
		String fileName = "题库数据数据模板.xlsx";
		try(ExcelExport ee = new ExcelExport("题库数据数据", TquestionBank.class, Type.IMPORT)){
			ee.setDataList(list).write(response, fileName);
		}
	}
	/**
	 * 导入用户数据
	 */
	@ResponseBody
	@RequiresPermissions("zx:question:bank:tquestionBank:edit")
	@PostMapping(value = "importData")
	public String importData(MultipartFile file, String updateSupport) {
		try {
			boolean isUpdateSupport = Global.YES.equals(updateSupport);
			String message = tquestionBankService.importData(file, isUpdateSupport);
			return renderResult(Global.TRUE, "posfull:"+message);
		} catch (Exception ex) {
			return renderResult(Global.FALSE, "posfull:"+ex.getMessage());
		}
	}
	/**
	 * 导出用户数据
	 */
	@RequiresPermissions("zx:question:bank:tquestionBank:view")
	@RequestMapping(value = "exportData")
	public void exportData(TquestionBank entity, Boolean isAll, String ctrlPermi, HttpServletResponse response) {
		
		List<TquestionBank> list = tquestionBankService.findList(entity);
		for(TquestionBank single:list){
			Integer objType = single.getObjType();
			String dictLabel = DictUtils.getDictLabel("t_exercises_query", objType.toString(), "未定义");
			single.setTypeName(dictLabel);
			String objAnswer = single.getObjAnswer();
			String answerName=DictUtils.getDictLabel("t_data_answer", objAnswer, "未定义");
			single.setAnswerName(answerName);
			String postCode = single.getPost();
			if(StringUtils.isNotBlank(postCode)){
				Post post= postService.get(postCode);
				single.setPostName(post.getPostName());
			}
			Integer category = single.getCategory();
			if(category!=null){
				String categoryName = DictUtils.getDictLabel("t_examination_category", category.toString(), "未定义");
				single.setCategoryName(categoryName);
			}
			Integer difficulty = single.getDifficulty();
			if(difficulty!=null){
				String difficultyName = DictUtils.getDictLabel("t_degree_difficulty", difficulty.toString(), "未定义");
				single.setDifficultyName(difficultyName);
			}
			Integer knowledgePointType = single.getKnowledgePointType();
			if(knowledgePointType!=null){
				String knowledgePointName = DictUtils.getDictLabel("t_knowledge_point_type", knowledgePointType.toString(), "未定义");
				single.setDifficultyName(knowledgePointName);
			}
		}
		String fileName = "题库数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
		try(ExcelExport ee = new ExcelExport("题库数据", TquestionBank.class)){
			ee.setDataList(list).write(response, fileName);
		}
	}
	public static void main(String[] args) {
		String a="ABC";
		if(a.length()>0){
			System.out.println(a.length());
		}
		char[] charArray = a.toCharArray();
		for(char b:charArray){
			System.out.println(b);
		}
		
	}
}